<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label="$t('base.code')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'code', validatorRules.code]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('base.name')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name', validatorRules.name]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('video.appKey')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'appKey', validatorRules.appKey]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>
        <a-form-item :label="$t('video.secret') " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'secret', validatorRules.appKey]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>


      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import {duplicateCheck } from '@/api/api'

  export default {
    name: "GlrVideoYsModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:this.$t('base.operation') ,
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {rules: [
            {required: true, message: this.$t('base.pleaseEnter')+'!'},
              {
                validator: this.validateVideoCode,
              }
          ]},
          name: {rules: [
            {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          appKey: {rules: [
            {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          secret: {rules: [
            {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          sysCompanyCode: {rules: [
          ]},
        },
        url: {
          add: "/glr/glrVideoYs/add",
          edit: "/glr/glrVideoYs/edit",
        }
      }
    },
    created () {
    },
    methods: {
      validateVideoCode(rule, value, callback){
        var params = {
          tableName: 'glr_video_ys',
          fieldName: 'code',
          fieldVal: value,
          dataId: this.model.id
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback( this.$t('video.ysCodeAlreadyExists') )
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'code','name','appKey','secret','sysCompanyCode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                //that.$message.success(res.message);
                that.$message.success(that.$t('base.operationSuccessful'));
                that.$emit('ok');
              }else{
                //that.$message.warning(res.message);
                that.$message.warning(that.$t('base.operationFailed'));
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','name','appKey','secret','sysCompanyCode'))
      },

      
    }
  }
</script>